package com.nono.controller;

import java.util.ArrayList;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nono.Dao.UserDao;

@Controller
public class loginController {
	@Autowired
	UserDao userDao;

	@RequestMapping(value="login",method = RequestMethod.POST)
	@ResponseBody
	public boolean loginPost(HttpServletRequest request, HttpServletResponse response) {
		String nameString = (String)request.getParameter("username");
		String password = (String)request.getParameter("password");
		//获取session;
		HttpSession session = request.getSession(true);
		ModelAndView mav;

		String correctPassword = userDao.getPassword(nameString);
		if( correctPassword.equals( password ) ) {
			//把这些东西都加到session中去;
			//在session的数据， 在界面中也可以直接通过${}获取到;
			session.setAttribute("name", nameString);
			session.setAttribute("id", userDao.getId(nameString) );
			if( 1==userDao.getRole(nameString) ) {
				session.setAttribute("role", 1 );
			}else{
				session.setAttribute("role", 0);
			}
			return true;
		}else{
			return false;
		}
	}
}
